import {Injectable} from '@angular/core';
import {_HttpClient} from '@delon/theme';
import {Observable} from 'rxjs/Observable';
import {Admin} from './admin';
import {Sysset} from './sysset';

@Injectable()
export class AdminService {

    url_admin_list = 'Admin/adminlist';
    url_admin_add = 'Admin/addadmin';
    url_admin_del = 'Admin/del/';
    url_admin_show = 'Admin/show/';
    url_admin_changeother = 'Admin/updatepwd/';
    url_admin_changepwd = 'Admin/updatepwd';

    url_admin_sysset_get = 'Admin/system';
    url_admin_sysset_post = 'Admin/system';

    constructor(private http: _HttpClient) {
    }


    getAdminList(page: number): Observable<any> {
        return this.http.get(this.url_admin_list, {page: page});
    }

    // addAdmin(item: Admin): Observable<string> {
    //     return this.http.post(this.url_admin_add, JSON.stringify(item));
    // }

    getSysSet(): Observable<any> {
        return this.http.get(this.url_admin_sysset_get);
    }

    setSysSet(Item: Sysset): Observable<any> {
        return this.http.post(this.url_admin_sysset_post, JSON.stringify(Item));
    }

    /**
     * 修改自己密码
     * @param {string} pwd
     * @returns {Observable<string>}
     */
    changeMinePwd(pwd: string): Observable<string> {
        return this.http.post<string>(this.url_admin_changepwd, pwd);
    }

    /**
     * 删除管理员
     * @param {string} id
     * @returns {Observable<string>}
     */
    delAdmin(id: string): Observable<{}> {
        return this.http.delete(this.url_admin_del + id);
    }

    /**
     * 添加/修改管理员
     * @param i
     * @returns {Observable<string>}
     */
    addAdmin(i): Observable<string> {
        const xx = {
            id: i.id ? i.id : 0,
            roleId: i.roleId,
            username: i.username,
            password: i.password,
        };
        return this.http.post<string>(this.url_admin_add, JSON.stringify(xx));
    }

    /**
     * 修改管理员密码
     * @param i
     * @returns {Observable<string>}
     */
    changePwd(id: number, pwd: string): Observable<string> {
        // const xx = {
        //   id: i.id ? i.id : 0,
        //   pwd: i.pwd,
        // };
        return this.http.post<string>(this.url_admin_changeother + id, pwd);
    }

    /**
     * 显示管理员详情
     * @param {string} id
     * @returns {Observable<{}>}
     */
    showAdmin(id: string): Observable<{}> {
        return this.http.get(this.url_admin_show + id);
    }
}
